package com.yeung.exam;

import java.util.*;

/**
 * abcdefghjklmnopqrstuvwxyz
 *
 *
 */
public class 外星字典排序 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String sorts = sc.nextLine();
        int count = Integer.parseInt(sc.nextLine());
        HashMap<Character, Integer> map = new HashMap<>();
        char[] chars = sorts.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            map.put(chars[i], i + 1);
        }

        while (count > 0) {
            String line = sc.nextLine();
            char[] part = line.toCharArray();
            ArrayList<Character> chs = new ArrayList<>();
            for (char c : part) {
                chs.add(c);
            }
            Collections.sort(chs, new Comparator<Character>() {
                @Override
                public int compare(Character o1, Character o2) {
                    Integer in1 = map.get(o1);
                    Integer in2 = map.get(o2);
                    return in1 - in2;
                }
            });
            StringBuffer sb = new StringBuffer();
            for (Character ch : chs) {
                sb.append(ch);
            }
            System.out.println(sb.toString());
            count--;
        }
    }

}
